from .common import *

'''
    模型创建数值集中处理模块
'''

'''
    读取 JSON 数据
'''
def read_field_options()->Dict[str, Any]:
    with open(FIELD_OPTIONS_JSON_PATH, encoding='utf-8') as f:
        data = json.load(f)
    return data

def read_field_types()->Dict[str, Any]:
    with open(FIELD_TYPES_JSON_PATH, encoding='utf-8') as f:
        data = json.load(f)
    return data

def read_meta_options()->Dict[str, Any]:
    with open(META_OPTIONS_JSON_PATH, encoding='utf-8') as f:
        data = json.load(f)
    return data

def read_field_conflict_names()->Dict[str, Any]:
    with open(CONFLICT_NAMES_JSON_PATH, encoding='utf-8') as f:
        data = json.load(f)
    return data

'''
    字段选项  field_options.json
'''
def get_all_field_options()->List[str]:
    '''获取所有的字段选项
    
        返回：所有的字段代码名 ['verbose_name', ...]
    '''
    return list(read_field_options().keys())

def get_all_field_options_by_key(key: str)->List[Tuple[Any]]:
    '''依据 key值 获取 field_options.json 的值集合
    
        返回：(键、名)二元组
    '''
    options = read_field_options()
    objs = []
    for k, v in options.items():
        if not v['deprecated']:
            objs.append((k, v.get(key),))
    return objs

def get_all_conflict_field_type_name()->List[str]:
    """获取所有需要冲突检验的字段类型名称"""
    check_fields = []
    temp = {}
    temp.update(read_field_options())
    for k, v in read_field_types().items():
        if v.get("extra_options"):
            temp.update(v.get("extra_options"))
    
    for k, v in temp.items():
        if v.get('links'):
            check_fields.append(k)
    
    return check_fields

'''
    字段类型  field_types.json
'''
def get_all_field_types()->List[str]:
    '''获取所有的字段类型
    
        代码名称
    '''
    return list(read_field_types().keys())

def get_all_field_types_by_key(key: str) -> List[Tuple[Any]]:
    '''依据 key值 获取 field_types.json 的值集合
    
        返回二元组 (key, value)
    '''
    types = read_field_types()
    objs = []
    for k, v in types.items():
        if not v['deprecated'] and v['inUse']:
            objs.append((k, v.get(key),))
    return objs

'''
    Meta 选项
'''
def get_all_meta_options()->List[str]:
    '''获取所有的Meta选项
    
        代码名称（仅键）
    '''
    return list(read_meta_options().keys())

def get_all_meta_options_by_key(key: str) -> List[Tuple[Any]]:
    '''依据 key值 获取 field_types.json 的值集合
    
        返回二元组 (key, value)
    '''
    types = read_meta_options()
    objs = []
    for k, v in types.items():
        if not v['deprecated']:
            objs.append((k, v.get(key),))
    return objs

